use generator_engine::tera::TeraEngine;
use model_graph_types::{
    assistant::{
        ability::AssistantAbility,
        chat::{ChatMessage, ChatOptions, ChatRole},
        prompts::PromptTemplate,
    },
    generator::CodeGenerator,
    modeling::Value,
};

use std::collections::HashMap;

pub async fn prompts_default_template(
    ability: AssistantAbility,
    _selector: HashMap<String, String>,
) -> anyhow::Result<Vec<PromptTemplate>> {
    match ability {
        AssistantAbility::Chat => {
            //
            return Ok(vec![PromptTemplate::new(
                &String::from("你是一名无所不能的专家,可以解决任何问题。"),
                &String::from("{{message.content}}"),
                &String::from(""),
            )?]);
        }
        _ => {
            tracing::debug!("未知的能力!{:?}", ability);
        }
    }
    //
    Ok(vec![])
}

pub async fn prompts_by_selector(
    ability: AssistantAbility,
    selector: HashMap<String, String>,
    question: Vec<ChatMessage>,
    options: HashMap<String, String>,
) -> anyhow::Result<Vec<ChatMessage>> {
    Ok(vec![])
}

pub async fn prompts_by_template(
    template: PromptTemplate,
    question: Vec<ChatMessage>,
    options: HashMap<String, String>,
) -> anyhow::Result<Vec<ChatMessage>> {
    //请求中是否有system
    let engine = TeraEngine::default();

    let mut result: Vec<ChatMessage> = vec![];
    let mut sys = question
        .iter()
        .filter(|item| item.role == ChatRole::System)
        .collect::<Vec<&ChatMessage>>();
    if let Some(sys_msg) = sys.pop() {
        //
        let content = engine
            .simple(
                Value::try_from(serde_json::json!({
                    "extensions": options
                }))?,
                serde_json::to_string(&sys_msg.content)?.as_str(),
            )
            .await?;
        result.push(ChatMessage::system(content));
    } else {
        //
        // let content = engine
        //     .simple(
        //         Value::try_from(serde_json::json!({
        //             "extensions": options
        //         }))?,
        //         template.system.clone().as_str(),
        //     )
        //     .await?;
        // result.push(ChatMessage::system(content));
    };

    // let user_template = &template.prompt;
    //
    // for message in question {
    //     match message.role {
    //         ChatRole::Assistant => {
    //             result.push(message);
    //         }
    //         ChatRole::System => {}
    //         ChatRole::User => {
    //             //
    //             let content = engine
    //                 .simple(
    //                     Value::try_from(serde_json::json!({
    //                         "message": message,
    //                         "extensions": options
    //                     }))?,
    //                     user_template.as_str(),
    //                 )
    //                 .await?;
    //             //
    //             result.push(ChatMessage::user(content));
    //         }
    //     }
    // }
    Ok(result)
}
